#!/usr/bin/env python
"""Handles reading in the configuration file.  Removes the need to
have a long command-line passed in.  Follows the LSB:
1) Machine configuration in /etc/opt/ce
2) User specific configuration in /$HOME/.ce
3) Current working directory
4) File specified by --conf parameter
"""
__author__   = 'Don B. Stringham'
__version__  = '0.01'
__date__     = '10/23/2006 07:12:34 AM'
#-IMPORTS----------------------------------------------------------------------
import os
import sys
import ConfigParser
from utils import *
#-CONSTANTS--------------------------------------------------------------------
CONF_FILE = 'ryce.conf'
MACHINE_DIR = convert_str('[sep]etc[sep]opt[sep]ryce[sep]') % str_dict
USER_DIR = convert_str('[home]/.ryce[sep]') % str_dict
CWD_DIR = '%s%s' % (os.getcwd(), os.sep)
#-CLASSES----------------------------------------------------------------------
class Conf:
    def __init__(self):
        """Look in the various locations for the ryce.conf file.  If found load
        it into memory.
        """
        self.__file = ''
        self.isfile = False
        self.config = ConfigParser.ConfigParser()
        #Look in /etc/opt/ryce
        if os.path.isdir(MACHINE_DIR):
            if os.path.isfile(MACHINE_DIR + CONF_FILE):
                self.__file = MACHINE_DIR + CONF_FILE
        #Look in /$HOME/.ryce
        if os.path.isdir(USER_DIR):
            if os.path.isfile(USER_DIR + CONF_FILE):
                self.__file = USER_DIR + CONF_FILE
        #Look in CWD
        if os.path.isdir(CWD_DIR):
            if os.path.isfile(CWD_DIR + CONF_FILE):
                self.__file = CWD_DIR + CONF_FILE
        if self.__file != '':
            self.config.read(self.__file)
            self.isfile = True
